package com.slz3.business.frame.task;

import com.slz3.business.frame.enums.ScheduleTimeType;
import com.slz3.business.module.messageManagement.mesCreate.domain.MesCreate;
import com.slz3.business.module.messageManagement.mesLog.domain.MesLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;
@Component
public class MessageCrontabExpression {
    @Autowired
    private  DynamicScheduleTaskSecond dynamicScheduleTaskSecond;
    public  void  CrontabToSchedule(MesCreate mesCreateDBLatest,MesCreate mesCreate){
        LocalDateTime serEffectiveTime = mesCreateDBLatest.getSerEffectiveTime();//生效时间
        LocalDateTime serTimePoint = mesCreateDBLatest.getSerTimePoint();//推送时间
        String serActivityType = mesCreateDBLatest.getSerActivityType().toString();//活动类型
        ScheduleTimeType serTimeType = mesCreateDBLatest.getSerTimeType();//时间类型
        String sendTo = mesCreateDBLatest.getSendTo();//模板编号
        StringBuilder cronTab = new StringBuilder();//cronTab表达式
        if(serTimeType!=null){//时间类型不为空
            String serTimeDetail = mesCreateDBLatest.getSerTimeDetail();//周几
            if(serTimePoint!=null){
                //设置推送时间点 时分秒
                int hour = serTimePoint.getHour();
                int mimute = serTimePoint.getMinute();
                int seconde = serTimePoint.getSecond();
                cronTab.append(String.valueOf(seconde)+" ").append(String.valueOf(mimute)+" ").append(String.valueOf(hour)+" ");
            }
            switch(serTimeType){
                case DAY ://每天执行
                    cronTab.append("* * ?");
                    break;
                case WEEK:
                    cronTab.append("? * ").append(serTimeDetail);
                    break;
                case MONTH:
                    cronTab.append(serTimeDetail+" * ?");
                    break;
            }
        }else{
            //执行一次 执行时间为生效时间
            int year = serEffectiveTime.getYear();
            Month month = serEffectiveTime.getMonth();
            int hour = serEffectiveTime.getHour();
            int minute = serEffectiveTime.getMinute();
            int second = serEffectiveTime.getSecond();
            cronTab.append(String.valueOf(second)+" ")
                    .append(String.valueOf(minute)+" ")
                    .append(String.valueOf(hour)+" ")
                    .append(String.valueOf(month)+" ")
                    .append(String.valueOf(year)+ " ");
        }
        //标题  推送内容
        String serTitle = mesCreateDBLatest.getSerTitle();
        String serEditorContent = mesCreateDBLatest.getSerEditorContent();
        String uuid = mesCreateDBLatest.getUuid().toString();
        ZonedDateTime zdt = serEffectiveTime.atZone(ZoneId.systemDefault());
        Date date = Date.from(zdt.toInstant());//生效日期
        dynamicScheduleTaskSecond.setCron(date,cronTab.toString(),uuid,serEditorContent,serTitle,sendTo);
        //生成log日志
        MesLog mesLog= new MesLog();

    }

    public  void delMessageSchedule(List<MesCreate> mesCreates){
        for (MesCreate mesCreate:mesCreates) {
            dynamicScheduleTaskSecond.stopCron(mesCreate.getUuid().toString());
            System.out.print("--------------删除定时器----------");
        }
    }
}
